﻿using sx_windows.dial;
using sx_windows.pane.about;
using sx_windows.pane.bulletin;
using sx_windows.pane.router;
using sx_windows.pane.wifi;
using sx_windows.presenter.dial;
using sx_windows.resource;
using sx_windows.utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sx_windows
{
    public partial class MainForm : Form
    {
        private DialContract.Presenter dialPresenter;
        private RouterSettingContract.Presenter routerPresenter;
        private WifiSettingContract.Presenter wifiPresenter;
        private BulletinContract.Presenter bulletinPresenter;
        private AboutContract.Presenter aboutPresenter;
        private NotifyIcon notifyIcon;
        private static bool hided;
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            InitIcon();
        }

        private void InitIcon()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Icon = this.Icon;
            notifyIcon.Text = " 嘘~ 让我安静的躺在这里！";
            notifyIcon.Visible = true;
            notifyIcon.Click += new System.EventHandler(this.Icon_Click);
            notifyIcon.BalloonTipIcon = ToolTipIcon.Info;
            notifyIcon.BalloonTipTitle = Const.APP_NAME;
            notifyIcon.BalloonTipText = Const.APP_NAME + "已隐藏到系统托盘！";
        }

        private void Icon_Click(object sender, EventArgs e)
        {
            if (!this.Visible|| this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
                this.ShowInTaskbar = true;
                this.Visible = true;
            }
            else
            {
                this.Visible = false;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitPresenters();
            InitView();
            InitMenuItem();
            InitFormEvent();
        }

        private void InitFormEvent()
        {
            this.FormClosing += this.MainForm_FormClosing;
        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                this.ShowInTaskbar = false;
                this.Visible = false;
                if (!hided)
                {
                    this.notifyIcon.ShowBalloonTip(200);
                    hided = true;
                }
                e.Cancel = true;
            }
            else
            {
                this.notifyIcon.Visible = false;
            }
        }

        private void InitMenuItem()
        {
            MenuItem exitMenu = new MenuItem("退出");
            exitMenu.Click += new System.EventHandler(this.ExitMenu_Click);
            notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { exitMenu });
        }

        private void ExitMenu_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void InitPresenters()
        {
            dialPresenter = new DialPresenter(this);
            routerPresenter = new RouterSettingPresenter(this);
            wifiPresenter = new WifiSettingPresenter(this);
            bulletinPresenter = new BulletinPresenter(this);
            aboutPresenter = new AboutPresenter(this);
        }
        private void InitView()
        {
            this.Text = Const.APP_NAME + " " + Const.APP_VERSION;
            dialPresenter.init();
            routerPresenter.init();
            wifiPresenter.init();
            bulletinPresenter.init();
            aboutPresenter.init();
        }
        public void ShowBottomText(string str)
        {
            this.bottomText.Text = str;
        }
        public void SetTabIndex(int index)
        {
            this.tabControl1.SelectedIndex = index;
        }


    }
}